package com.shujia.flink.core

import org.apache.flink.api.scala._

object Demo1BatchWC {
  def main(args: Array[String]): Unit = {


    /**
      *
      * 构建flink的环境
      *
      */

    val env: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment


    //读取数据
    val lines: DataSet[String] = env.readTextFile("flink/data/words.txt")


    //将单词压扁
    val words: DataSet[String] = lines.flatMap(line => line.split(","))


    //转换成kvg格式
    val kv: DataSet[(String, Int)] = words.map(word => (word, 1))

    //按照单词进行分组
    val groupDS: GroupedDataSet[(String, Int)] = kv.groupBy(0)

    //对第二列求和
    val countDS: AggregateDataSet[(String, Int)] = groupDS.sum(1)

    countDS.print()


  }
}
